package com.ikun.appointment.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.appointment.service.ConventionService;
import com.ikun.common.entity.Convention;
import com.ikun.common.entity.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 挂号预约表(Convention)表控制层
 *
 * @author makejava
 * @since 2024-02-26 10:08:25
 */
@Slf4j
@RestController
@RequestMapping("convention")
public class ConventionController {

    /**
     * 服务对象
     */
    @Resource
    private ConventionService conventionService;

    /**
     * 查询所有
     */
    @RequestMapping("list")
    public ResponseResult<List<Convention>> findAll() {
        return ResponseResult.ok(conventionService.list());
    }

    /**
     * 根据用户id获取总条数
     */
    @GetMapping("count")
    public ResponseResult<Integer> count(Integer userId,Integer state) {
        userId = userId == null ? 1 : userId;
        return ResponseResult.ok(conventionService.findCount(userId,state));
    }


    /**
     * 查询预约表相关信息并分页
     */
    @GetMapping("page")
    public ResponseEntity<Page<Convention>> selectPage(
            @RequestParam(required = false, defaultValue = "1") Long current,
            @RequestParam(required = false, defaultValue = "5") Long size,
            Integer userId, Integer state) {
        Page<Convention> page;
        try {
            userId = userId == null ? 1 : userId;
            page = conventionService.findPage(current, size, userId, state);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
//        log.info("查询成功，查询结果为：" + page.getRecords());
        return ResponseEntity.ok(page);
    }


    /**
     * 删除预约信息
     */
    @DeleteMapping("delete/{id}")
    public ResponseResult<Integer> delete(@PathVariable Integer id) {
        conventionService.deleteById(id);
        return ResponseResult.ok(200);
    }




}

